from collections import defaultdict

x = int(input())
import heapq

heap = [(0, x)]
distance = defaultdict(int)
distance[x] = 0
while heap:
    dis, val = heapq.heappop(heap)
    if not val:
        print(dis)
        break
    if distance[val] < dis:
        continue
    # 两种操作
    nx = val + 1
    if distance.get(nx, float('inf')) > dis + 1:
        distance[nx] = dis + 1
        heapq.heappush(heap, (dis + 1, nx))
    # 操作二
    nx = val & (val + 1)
    if distance.get(nx, float('inf')) > dis + 1:
        distance[nx] = dis + 1
        heapq.heappush(heap, (dis + 1, nx))
